Skip to content

cuda::isclose()#9577

Open
fbusato wants to merge 16 commits into
NVIDIA:mainfrom
fbusato:isclose
Open

cuda::isclose()#9577
fbusato wants to merge 16 commits into
NVIDIA:mainfrom
fbusato:isclose

Conversation

@fbusato

@fbusato fbusato commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Description

Comparing two floating-point values is a widely used functionality. Every library that use floating-point values need to compare them at some point. CCCL used it too for internal testing.

Python has math.isclose() https://peps.python.org/pep-0485/, and numpy.isclose.
Several C++ testing frameworks and libraries provide a similar utility, GoogleTest, Catch2, Boost.Math, Eigen.

This PR introduces 3 overloads for isclose() following implementation decisions of math.isclose()

bool isclose(T lhs, T rhs);
bool isclose(T lhs, T rhs, float relative_tolerance);
bool isclose(T lhs, T rhs, float relative_tolerance, T absolute_tolerance);

we could also think to add interfaces based on C++20 designed initializer.

Supported types: integers, (extended) floating-point, complex (std. cuda, cuda::std).

Todo: documentation (currently a draft)
Requires:

@fbusato fbusato self-assigned this Jun 24, 2026
@fbusato fbusato added the libcu++ For all items related to libcu++ label Jun 24, 2026
@fbusato fbusato requested review from a team as code owners June 24, 2026 01:29
@fbusato fbusato requested a review from gonidelis June 24, 2026 01:29
@fbusato fbusato added this to CCCL Jun 24, 2026
@fbusato fbusato requested a review from Jacobfaib June 24, 2026 01:29
@github-project-automation github-project-automation Bot moved this to Todo in CCCL Jun 24, 2026
@cccl-authenticator-app cccl-authenticator-app Bot moved this from Todo to In Review in CCCL Jun 24, 2026
Comment thread docs/libcudacxx/extended_api/numeric/isclose.rst
@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 548dfd63-db47-4b91-a13c-56bce3943b58

📥 Commits

Reviewing files that changed from the base of the PR and between 0fed909 and 5ed5965.

📒 Files selected for processing (1)
  • docs/libcudacxx/extended_api/numeric/isclose.rst
🚧 Files skipped from review as they are similar to previous changes (1)
  • docs/libcudacxx/extended_api/numeric/isclose.rst

Note: CodeRabbit is enabled on this repository as a convenience for maintainers and contributors. Use your best judgment when considering its review comments and
suggestions — a suggested change may be inadequate, unnecessary, or safe to ignore.
Contributors are not expected to address every comment. Human reviews are what
ultimately matter for merging.

Summary

This PR adds cuda::isclose() (modeled after Python’s math.isclose() / cmath.isclose() as part of the work tracking #9576) for approximate equality of numeric values, providing scalar and complex overloads based on relative tolerance and (where supported) an absolute tolerance.

Changes

Implementation (libcudacxx/include/cuda/__numeric/isclose.h)

  • Added a new cuda/__numeric/isclose.h header implementing:
    • Scalar overloads
      • isclose(lhs, rhs) (integers: exact equality; floating/extended: default relative tolerance)
      • isclose(lhs, rhs, rel_tol)
      • isclose(lhs, rhs, rel_tol, abs_tol) (integers/floating/extended)
    • Complex overloads (__host__ __device__)
      • isclose(lhs, rhs, rel_tol, abs_tol) with participation constrained so abs_tol matches typename Complex::value_type
      • isclose(lhs, rhs, rel_tol)
      • isclose(lhs, rhs)
  • Key behavior:
    • Default relative tolerance for extended/floating types is computed from numeric_limits<_Tp>::max_digits10 using a base-10 exponent derived from half that digit count.
    • Validates:
      • rel_tol is in [0.0, 1.0]
      • abs_tol is finite and non-negative (with the signed-integer non-negativity check gated to signed integer types)
    • Special-value handling:
      • Returns true on exact equality (and for complex, when real and imaginary parts are exactly equal).
      • Returns false if either operand/component is non-finite (NaN/Infinity cases).
    • Approximate-equality criteria:
      • Floating-point / extended: abs(lhs - rhs) <= max(abs_tol, rel_tol * max(abs(lhs), abs(rhs)))
      • Integers: uses an overflow-safe unsigned absolute-difference helper, then applies the same tolerance threshold comparison.
      • Complex: otherwise compares Euclidean distance computed with hypot against max(abs_tol, rel_tol * max(|lhs|, |rhs|)), where |z| is computed via hypot.

Umbrella header integration (libcudacxx/include/cuda/numeric)

  • Added cuda/__numeric/isclose.h to libcudacxx/include/cuda/numeric.

Documentation

  • Added docs/libcudacxx/extended_api/numeric/isclose.rst documenting:
    • all scalar and complex overloads
    • tolerance parameter semantics, defaults (notably omitted abs_tol behavior), and constraints
    • the NaN/Infinity rules and the approximate-equality criterion used for scalar/complex values
    • a CUDA example kernel and main with assertions
  • Updated docs/libcudacxx/extended_api/numeric.rst:
    • added the numeric/isclose page to the hidden toctree
    • added a cuda::isclose entry row including availability metadata (CCCL 3.5.0, CUDA Toolkit 13.5).

Testing (libcudacxx/test/libcudacxx/cuda/numeric/isclose/isclose.pass.cpp)

  • Added a new pass test covering:
    • compile-time checks for overload availability, return type, and noexcept
    • runtime checks across floating-point, integral, and complex types (including NaN/Infinity)
    • SFINAE/trait-based negative tests for invalid complex abs_tol combinations
    • feature-gated runtime coverage for extended types (e.g., half/bfloat16) and host-only std::complex using existing target/feature macros.

Walkthrough

Adds cuda::isclose for scalar and complex approximate-equality checks, wires it into cuda/numeric, and adds matching documentation and coverage tests.

Changes

cuda::isclose numeric utility

Layer / File(s) Summary
Documentation
docs/libcudacxx/extended_api/numeric.rst, docs/libcudacxx/extended_api/numeric/isclose.rst
Adds the new reference page, documents overloads and semantics, and registers the page in the numeric navigation and availability table.
Internal helpers
libcudacxx/include/cuda/__numeric/isclose.h
Defines the comparison-type selector, default relative tolerance, overflow-safe integer difference, and the floating-point, complex, and integer implementations.
Public overloads and header wiring
libcudacxx/include/cuda/__numeric/isclose.h, libcudacxx/include/cuda/numeric
Adds the public scalar and complex overloads, closes the namespace and include guard, and includes the new header from cuda/numeric.
Test coverage
libcudacxx/test/libcudacxx/cuda/numeric/isclose/isclose.pass.cpp
Adds compile-time and runtime tests for floating-point, integral, and complex overloads, including invalid abs_tol cases and target-gated runtime coverage.

Comment @coderabbitai help to get the list of available commands.

@github-actions

This comment has been minimized.

@davebayer davebayer changed the title cuda::std::isclose() cuda::isclose() Jun 24, 2026
Comment thread libcudacxx/include/cuda/__numeric/isclose.h Outdated
Comment thread libcudacxx/include/cuda/__numeric/isclose.h Outdated
Comment thread libcudacxx/include/cuda/__numeric/isclose.h
Comment thread libcudacxx/include/cuda/__numeric/isclose.h
Comment thread libcudacxx/test/libcudacxx/cuda/numeric/isclose/isclose.pass.cpp
fbusato and others added 2 commits June 24, 2026 09:54
Co-authored-by: Jacob Faibussowitsch <jacob.fai@gmail.com>
@github-actions

This comment has been minimized.

@fbusato fbusato requested review from a team as code owners June 26, 2026 19:48
@fbusato fbusato requested review from a team as code owners June 26, 2026 19:48
@fbusato fbusato requested review from miscco and rwgk June 26, 2026 19:48
@copy-pr-bot

copy-pr-bot Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

Comment thread temp.build.yml Outdated
@fbusato fbusato requested a review from davebayer June 26, 2026 20:27
@github-actions

Copy link
Copy Markdown
Contributor

😬 CI Workflow Results

🟥 Finished in 2h 20m: Pass: 99%/120 | Total: 2d 17h | Max: 2h 20m | Hits: 75%/587960

See results here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libcu++ For all items related to libcu++

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

3 participants